package com.hqyj.role.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.hqyj.config.MyMetaObjectHandler;
import com.hqyj.role.dto.RoleInfo;
import com.hqyj.role.entity.Role;
import com.hqyj.role.entity.RolePower;
import com.hqyj.role.mapper.RoleMapper;
import com.hqyj.role.service.RoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hqyj.user.pojo.MyNode;
import com.hqyj.utils.ResultUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author sdx2009
 * @since 2022-10-08
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Resource
    private RoleMapper roleMapper;

    @Override
    public ResultUtils SelectRolePower() {
        List<Role> roles = roleMapper.queryRoleAndPower();
        return new ResultUtils(200,"",roles);
    }

    public  ResultUtils queryPowerByRole(RoleInfo roleInfo){
        List<MyNode> myNodes = roleMapper.queryPowerByRole(roleInfo.getRoleName());
        List<MyNode> list = findChildren(myNodes);
        return new ResultUtils(200,"",list);
    }

    @Override
    public ResultUtils queryAllPower() {
        List<MyNode> myNodes = roleMapper.queryAllPower();
        List<MyNode> list = findChildren(myNodes);
        return new ResultUtils(200,"",list);
    }

    @Transactional
    @Override
    public ResultUtils updateRole(RoleInfo roleInfo) {
        Role role = new Role();
        BeanUtils.copyProperties(roleInfo,role);
        UpdateWrapper<Role> uw = new UpdateWrapper<>();
        uw.eq("role_id",roleInfo.getRoleId());
        if (this.update(role,uw)){
            roleMapper.deletePowerByRoleId(roleInfo.getRoleId());
            List<RolePower> list = new ArrayList<>();
            for (Integer powerId : roleInfo.getPowerList()){
                RolePower rolePower = new RolePower();
                rolePower.setPowerId(powerId);
                rolePower.setRoleId(roleInfo.getRoleId());
                list.add(rolePower);
            }
            roleMapper.insertPower(list);
            return new ResultUtils(200,"修改成功!",null);
        }
        return new ResultUtils(500,"修改失败!",null);
    }

    @Transactional
    @Override
    public ResultUtils addRole(RoleInfo roleInfo) {
        Role role = new Role();
        BeanUtils.copyProperties(roleInfo,role);
        role.setRoleId(null);
        this.save(role);
        QueryWrapper<Role> qw = new QueryWrapper<>();
        qw.eq("role_name",role.getRoleName());
        Integer roleId = this.getOne(qw).getRoleId();
        List<RolePower> list = new ArrayList<>();
        for (Integer powerId : roleInfo.getPowerList()){
            RolePower rolePower = new RolePower();
            rolePower.setRoleId(roleId);
            rolePower.setPowerId(powerId);
            list.add(rolePower);
        }
        roleMapper.insertPower(list);
        return new ResultUtils(200,"添加成功！",null);
    }

    @Override
    public ResultUtils deleteRole(RoleInfo roleInfo) {
        this.removeById(roleInfo.getRoleId());
        roleMapper.deletePowerByRoleId(roleInfo.getRoleId());
        return new ResultUtils(200,"删除成功！",null);
    }

    public List<MyNode> findChildren(List<MyNode> myNodes){
        List<MyNode> list = new LinkedList<>();
        for (int i = 0;i < myNodes.size();i++){
            if (myNodes.get(i).getParentId() == 0){
                List<MyNode> listChildren = new LinkedList<>();
                for (int j = 0;j < myNodes.size();j++){
                    if (myNodes.get(j).getParentId() == myNodes.get(i).getNodeId()){
                        listChildren.add(myNodes.get(j));
                    }
                }
                myNodes.get(i).setChildren(listChildren);
                list.add(myNodes.get(i));
            }
        }
        return list;
    }
}
